package com.calvin.study.config.security;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import com.calvin.study.base.ResponseModel;

//SavedRequestAwareAuthenticationSuccessHandler提供了记录上一次登录成功后跳转的页面。
@Component
public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
	
	@Value("${spring.security.logintype}")
	private String loginType;

	@Override
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
			Authentication authentication) throws ServletException, IOException {
		if(loginType.equalsIgnoreCase("JSON")) {
			response.setContentType("application/json;charset=UTF-8");
			response.getWriter().write(ResponseModel.success().toJson());
		}else {
			super.onAuthenticationSuccess(request, response, authentication);
		}
	}
	
	

}
